package com.allen.app.common.utils;

import com.alibaba.fastjson.util.TypeUtils;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import lombok.experimental.UtilityClass;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * servlet工具类
 *
 * @author LiMiaojun
 * @date 2017年7月17日 上午9:56:04
 */
@UtilityClass
public class ServletContextUtil {

    public HttpSession getSession() {
        return Objects.requireNonNull(getRequest()).getSession();
    }

    private ServletRequestAttributes getRequestAttributes() {
        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    }

    public HttpServletRequest getRequest() {

        return Try.tryCatch(() -> getRequestAttributes().getRequest(), null);
    }

    public HttpServletResponse getResponse() {
        return Try.tryCatch(() -> getRequestAttributes().getResponse(), null);
    }

    /**
     * 获取本地根路径
     */
    public String getRootPath() {
        HttpServletRequest request = getRequest();
        if (Objects.isNull(request)) {
            return "";
        }
        return request.getScheme().concat("://").concat(request.getServerName())
            .concat(":" + request.getServerPort());
    }

    /**
     * 登陆用户
     */
    public final String LOGIN_USER = "loginUser";

    /**
     * 判断ip地址是否是内网.
     *
     * @param ip ip地址
     * @return boolean
     * @author Limiaojun
     * @date 2019/8/21 11:24
     */
    public boolean internalIp(String ip) {
        return Objects.equals(ip.substring(0, 3), "10.") || Objects.equals(ip.substring(0, 4), "172.") || Objects
            .equals(ip.substring(0, 4), "192.");
    }

    /**
     * 获取ip地址.
     *
     * @return java.lang.String
     * @author Limiaojun
     * @date 2019/8/21 11:25
     */
    public String getIpAddress() {
        HttpServletRequest request = getRequest();
        String ip = request.getHeader("X-Real-IP");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("x-forwarded-for");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

//    /**
//     * Description: 根据名称获取请求body参数
//     *
//     * @author Limiaojun
//     * @date 2019/9/6 9:15
//     **/
//    public <T> T getBodyParamByName(String paramName, Class<T> clazz) {
//        HttpServletRequest request = getRequest();
//        if (Objects.isNull(request)) {
//            return null;
//        }
//        try {
//            String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
//            JSONObject res = JSONObject.parseObject(body);
//
//            return res.getObject(paramName, clazz);
//        } catch (IOException e) {
//            return null;
//        }
//    }

    /**
     * Description: 根据名称获取Param参数
     *
     * @author Limiaojun
     * @date 2019/9/6 9:15
     **/
    public <T> T getParamByName(String paramName, Class<T> clazz) {
        HttpServletRequest request = getRequest();
        if (Objects.isNull(request)) {
            return null;
        }
        String res = request.getParameter(paramName);
        return TypeUtils.castToJavaBean(res, clazz);
    }

}

